package com.example.mcu.serial;

import java.io.File;
import java.nio.ByteBuffer;





public class SerialSend {
	private static Serial serial;
	private static SerialSend serialSend;
	private SerialSend(){
		serial=Serial.getInstance();
		//serial.setHandler(new Handler());
		serial.start();	
		
	}
	
	public static SerialSend getInstance(){
		if(serialSend==null){
			serialSend=new SerialSend();
		}
		return serialSend;
	}

	public static final int Serial_HARDWAREVERSION=1;
	public static final int Serial_SHUTDOWN=2;
	public static final int Serial_SHUTDOWNTOOPEN_TIME=3;
	public static final int Serial_SHUTDOWNTOOPEN_TIME_CANCEL=4;
	public static final int Serial_SLEEPING=5;
	public static final int Serial_WAKE=6;
	public static final int Serial_OPEN=7;
	public static final int Serial_CHOOSEEQ=8;
	public static final int Serial_FILEEQ=9;
	public static final int Serial_VOLUEM_NUMBER=10;
	public static final int Serial_POWER_CHARGE=11;
	public static final int Serial_POWER_FULL=12;
	public static final int Serial_POWER_PERCENT=13;
	public static final int Serial_POWER_EXTERNAL=14;
	public static final int Serial_DISTANCE=15;
	public static final int Serial_ANSWER=16;
    public static final int Serial_WATCHDOG_TIMER=17;
    public static final int Serial_BOOTLOADER=18;

	public void sendStatus(int j){
		Pack p=new Pack();
		switch (j) {
		case 1:
			p.setType(Type.S_HARDWARE_VERSION);
			serial.send(p);
			break;
		case 2:
			p.setType(Type.S_SHUTDOWN);
			serial.send(p);
			break;
		case 3:
			p.setType(Type.S_SHUTDOWNTOOPEN_TIME);
         	byte[] b3=Pack.intToByteArray(10000);
         	p.setData(b3);
			serial.send(p);
			break;
		case 4:
			p.setType(Type.S_SHUTDOWNTOOPEN_TIME_CANCEL);
			serial.send(p);
			break;
		case 5:
			p.setType(Type.S_SLEEPING);
			serial.send(p);
			break;
		case 6:
			p.setType(Type.S_WAKE);
			serial.send(p);
			break;
		case 7:
			p.setType(Type.S_OPEN);
			serial.send(p);
			break;
		case 8:
			p.setType(Type.S_CHOOSEEQ);
         	byte[] b8=new byte[1];
         	b8[0]=Type.EQ02;
         	p.setData(b8);
			serial.send(p);
			break;
		case 9:
		 	p.setType(Type.S_FILEEQ);
         	p.setData(Type.EQFile1);
			serial.send(p);
			break;
		case 10:
			p.setType(Type.S_VOLUEM_NUMBER);
         	p.setData(Pack.intToBytes2(0));
			serial.send(p);
			break;
		case 11:
			p.setType(Type.S_POWER_CHARGE);
			serial.send(p);
			break;
		case 12:
			p.setType(Type.S_POWER_FULL);
			serial.send(p);
			break;
		case 13:
			p.setType(Type.S_POWER_PERCENT);
			serial.send(p);
			break;
		case 14:
			p.setType(Type.S_POWER_EXTERNAL);
			serial.send(p);
			break;
		case 15:
			p.setType(Type.S_DISTANCE);
         	byte[] b15=new byte[1];
         	b15[0]=1;
         	p.setData(b15);
			serial.send(p);
			break;
		case 16:
			p.setType(Type.SM_ANSWER);
         	byte[] b=new byte[1];
         	b[0]=1;
         	p.setData(b);
			serial.send(p);
			break;
		case 17:
			p.setType(Type.S_WATCHDOG_TIMER);
			serial.send(p);
			break;
		case 18:
			p.setType(Type.S_BOOTLOADER);
			serial.send(p);
			break;

		default:
			break;
		}
	}
	
	public void sendTime(int time){
		Pack p=new Pack();
		p.setType(Type.S_SHUTDOWNTOOPEN_TIME);
		p.setData(Pack.int2byteArray(time));
		serial.send(p);
	}
	
	public void sendVolume(int volume){
		 Pack p=new Pack();
		 p.setType(Type.S_VOLUEM_NUMBER);
		 int a=0x00+volume;
		 ByteBuffer buffer=ByteBuffer.allocate(1);
		 buffer.put((byte)a);
		 p.setData(buffer.array());
		 serial.send(p);
	}
	
	public void ChooseEQ(byte b){
		 Pack p=new Pack();
		p.setType(Type.S_CHOOSEEQ);
     	byte[] b8=new byte[1];
     	b8[0]=b;
     	p.setData(b8);
		serial.send(p);
	}
	
	public void sendEQFile(File f){
		Pack p=new Pack();
		p.setType(Type.S_FILEEQ);
     	p.setData(Type.EQFile1);
		serial.send(p);
	}
	
	
	public void close(){
		
		serial.stop();
		serial.closeSerialPort();
		
	}
	
}
